package cn.col.gszf.controller.root;

import cn.col.gszf.model.RestResult;
import cn.col.gszf.pojo.Vote;
import cn.col.gszf.service.RedisService;
import cn.col.gszf.service.VoteService;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 用户投票入口
 * @Author zhaoning
 * @Date 2024/6/24
 */

@Controller
@RequestMapping("/root/vote")
public class RootVoteController {
    @Resource
    private VoteService votesService;

    @Resource(name = "redis-service")
    private RedisService redisService;

    @GetMapping("/{voteId}")
    @ResponseBody
    public RestResult<Vote> get(@PathVariable Integer voteId) {
        Vote vote = votesService.findById(voteId);
        String votesJson = JSONUtil.toJsonStr(vote);
        return RestResult.success(200, votesJson);
    }

    @DeleteMapping("/{voteId}")
    @ResponseBody
    public RestResult<Boolean> deleteVote(@PathVariable Integer voteId) {
        Vote vote = votesService.findById(voteId);
        votesService.deleteVoteById(voteId);
        redisService.removeVote(vote);
        return RestResult.success(200, Boolean.TRUE);
    }
}
